/*
 * ViewTable.cpp
 *
 *  Created on: 2011-1-7
 *      Author: Administrator
 */

#include "ViewTable.h"
#include "ViewTableRow.h"
#include "TitleItem.h"

CViewTable::CViewTable()
	{
	iTableData = NULL;
	}

CViewTable::~CViewTable()
	{
	iTableData = NULL;
	}

CViewTable* CViewTable::NewL(const TRect& aRect, const CCoeControl* aParent)
	{
	CViewTable* self = CViewTable::NewLC(aRect, aParent);
	CleanupStack::Pop(self);
	return self;
	}

CViewTable* CViewTable::NewLC(const TRect& aRect, const CCoeControl* aParent)
	{
	CViewTable* self = new(ELeave) CViewTable();
	CleanupStack::PushL(self);
	self->ConstructL(aRect, aParent);
	return self;
	}

void CViewTable::Draw(const TRect& aRect) const
	{
	CListBox::Draw(aRect);
	CGraphicsTool::DrawRect(SystemGc(),aRect,KRgbWhite);
	}

void CViewTable::setTableData(CViewTableData* aTableData)
	{
	iTableData = aTableData;
	}

CViewTableData* CViewTable::TableData()
	{
	return iTableData;
	}

void CViewTable::RefreshTable()
	{
	RemoveAllItems();
	CViewTableRow* row = NULL;
	if (iTableData)
		{
		if (iTableData->Title().Length() > 0)
			{
			CTitleItem* title = CTitleItem::NewL(this);
			title->SetTitleColor(KRgbRed);
			title->SetTitleL(iTableData->Title());
			title->SetLayout();
			AddListItemL(title);
			}
		if (iTableData->iHeaderRowData && iTableData->iHeaderRowData->Count() > 0)
			{
			row = CViewTableRow::NewL(this,iTableData->iHeaderRowData);
			row->setColumnWidths(iTableData->iColumnWidths);
			row->SetTitleColor(KRgbYellow);
			row->SetLayout();
			AddListItemL(row);
			}
		for (TInt i = 0; i < iTableData->Count(); i++)
			{
			row = CViewTableRow::NewL(this,iTableData->getDataRow(i));
			row->setColumnWidths(iTableData->iColumnWidths);
			row->SetTitleColor(KRgbWhite);
			row->SetLayout();
			AddListItemL(row);
			}
		UpdateControls();
		}
	TRect rect = Rect();
	rect.SetHeight(iTotalHeight + 20);
	SetRect(rect);
	//DrawNow();
	}
